package com.bw.mvp.ui;

import com.bw.mvp.base.BasePresenter;
import com.bw.mvp.model.BaseResponse;
import com.bw.mvp.model.PostListModel;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PostPresenter extends BasePresenter<PostContract.IPostView> implements PostContract.IPostPresenter {
    private PostContract.IPostModel mPostModel;
    public PostPresenter() {
        mPostModel = new PostModel();
    }
    @Override
    public void getPostList(int pageNum, int pageSize) {
        //2：presenter调用model请求数据
        mPostModel.getPostList(pageNum,pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<PostListModel>>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {

                    }

                    @Override
                    public void onNext(BaseResponse<PostListModel> postListModelBaseResponse) {
                        //3:presenter将model请求的数据通过接口回调回传给view
                        if (isViewAttached()){
                            getView().getPostListSuccess(postListModelBaseResponse.data);
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        if (isViewAttached()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
